package org.ymjava.medical.admin.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ymjava.medical.admin.annotation.LoginAdmin;
import org.ymjava.medical.admin.service.AdminTokenManager;
import org.ymjava.medical.core.util.ResponseUtil;
import org.ymjava.medical.core.util.bcrypt.BCryptPasswordEncoder;
import org.ymjava.medical.db.AllPackage.biz.NurseAdminBiz;
import org.ymjava.medical.db.AllPackage.biz.SysPermBiz;
import org.ymjava.medical.db.AllPackage.biz.SysRoleBiz;
import org.ymjava.medical.db.AllPackage.entity.NurseAdmin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
public class AdminAdminController {
	@Autowired
	private NurseAdminBiz adminService;

	@Autowired
	private SysRoleBiz litemallRoleService;

	@Autowired
	private SysPermBiz litemallSysPermService;

	@GetMapping("/info")
	public Object info(String token) {
		Integer adminId = AdminTokenManager.getUserId(token);

		if (adminId == null) {
			return ResponseUtil.badArgumentValue();
		}
		NurseAdmin admin = adminService.selectById(adminId);
		if (admin == null) {
			return ResponseUtil.badArgumentValue();
		}

		Map<String, Object> data = new HashMap<>();
		data.put("name", admin.getUsername());
		data.put("avatar", admin.getAvatar());

		// 查询roles
		String adminIds = String.valueOf(adminId);
		List<Map> role = litemallRoleService.getUserRole(adminIds);

		// 查询权限
		List<Map> perms = litemallSysPermService.getPermsByUserId(adminId);

		data.put("roles", role);
		data.put("perms", perms);
		data.put("introduction", "admin introduction");
		return ResponseUtil.ok(data);
	}

	@GetMapping("/list")
	public Object list(
		@LoginAdmin Integer adminId,
		String username,
		@RequestParam(value = "page", defaultValue = "1") Integer page,
		@RequestParam(value = "limit", defaultValue = "10") Integer limit,
		String sort,
		String order) {
		if (adminId == null) {
			return ResponseUtil.unlogin();
		}
		return ResponseUtil.ok(adminService.querySelective(username, page, limit, sort, order));
	}

	@GetMapping("/totalList")
	public Object totalList(
		@LoginAdmin Integer adminId, String username, String sort, String order) {

		List<NurseAdmin> adminList = adminService.queryTotalSelective(username, sort, order);

		Map<String, Object> data = new HashMap<>();
		data.put("items", adminList);

		return ResponseUtil.ok(data);
	}

	@PostMapping("/create")
	public Object create(@LoginAdmin Integer adminId, @RequestBody NurseAdmin admin) {
		if (adminId == null) {
			return ResponseUtil.unlogin();
		}

		String rawPassword = admin.getPassword();
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		String encodedPassword = encoder.encode(rawPassword);
		admin.setPassword(encodedPassword);
		admin.setAddTime(new Date());
		String username = admin.getUsername();
		username.replaceAll(" ", "");
		admin.setUsername(username);
		adminService.insert(admin);
		return ResponseUtil.ok(admin);
	}

	@GetMapping("/read")
	public Object read(@LoginAdmin Integer adminId, Integer id) {
		if (adminId == null) {
			return ResponseUtil.unlogin();
		}

		if (id == null) {
			return ResponseUtil.badArgument();
		}

		NurseAdmin admin = adminService.selectById(id);
		return ResponseUtil.ok(admin);
	}

	@PostMapping("/update")
	public Object update(@LoginAdmin Integer adminId, @RequestBody NurseAdmin admin) {
		if (adminId == null) {
			return ResponseUtil.unlogin();
		}

		Integer anotherAdminId = admin.getId();
		if (anotherAdminId.intValue() == 1) {
			return ResponseUtil.fail(403, "超级管理员不能修改");
		}

		String username = admin.getUsername();
		String password = admin.getPassword();
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		String encodedPassword = encoder.encode(password);
		admin.setPassword(encodedPassword);
		username.replaceAll(" ", "");
		admin.setUsername(username);
		adminService.updateById(admin);
		return ResponseUtil.ok(admin);
	}

	@PostMapping("/delete")
	public Object delete(@LoginAdmin Integer adminId, @RequestBody NurseAdmin admin) {
		if (adminId == null) {
			return ResponseUtil.unlogin();
		}

		Integer anotherAdminId = admin.getId();
		if (anotherAdminId.intValue() == 1) {
			return ResponseUtil.fail(403, "超级管理员不能删除");
		}
		adminService.deleteById(anotherAdminId);
		return ResponseUtil.ok();
	}
}
